package com.neueedu.android.ticket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.neueedu.android.R;
import com.neueedu.android.entity.Ticket;

import java.util.ArrayList;
import java.util.List;

public class TicketActivity2 extends AppCompatActivity {

    TextView fromandto;
    ListView lvTicketList;

    List<Ticket> data = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket2);

        fromandto = findViewById(R.id.fromandto);
        lvTicketList = findViewById(R.id.lvTicketList);

        final Intent intent = getIntent();
        String from = intent.getStringExtra("from");
        String to = intent.getStringExtra("to");


        fromandto.setText(from + "--" + to);


        //填充ListView

        initData();
        //创建适配器
        MyTicketAdapter adapter = new MyTicketAdapter(data);

        //绑定适配器
        lvTicketList.setAdapter(adapter);
        lvTicketList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Ticket ticket = data.get(position);

                Intent intent = new Intent(TicketActivity2.this,TicketActivity3.class);
                intent.putExtra("ticket",ticket);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        for (int i = 0; i < 6; i++) {
            data.add(new Ticket("G158" + i, "14:0" + i, "22:0" + i, true, true, i + "日"));
        }
    }

    class MyTicketAdapter extends BaseAdapter {

        private List<Ticket> ticketList;


        public MyTicketAdapter(List<Ticket> ticketList) {
            this.ticketList = ticketList;
        }

        @Override
        public int getCount() {
            return ticketList.size();
        }

        @Override
        public Object getItem(int position) {
            return ticketList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            Ticket data = (Ticket) getItem(position);
            ViewHolder viewHolder = null;
            if (convertView == null) {
                v = getLayoutInflater().inflate(R.layout.activity_ticket2_ticket_item, null);
                viewHolder = new ViewHolder();
                viewHolder.ticketName = v.findViewById(R.id.ticketName);
                viewHolder.ivFlagStart = v.findViewById(R.id.ivFlagStart);
                viewHolder.ivFlagEnd = v.findViewById(R.id.ivFlagEnd);
                viewHolder.tvStartTime = v.findViewById(R.id.tvStartTime);
                viewHolder.tvEndTime = v.findViewById(R.id.tvEndTime);
                viewHolder.tvSeat1 = v.findViewById(R.id.tvSeat1);
                viewHolder.tvSeat2 = v.findViewById(R.id.tvSeat2);
                viewHolder.tvSeat3 = v.findViewById(R.id.tvSeat3);
                viewHolder.tvSeat4 = v.findViewById(R.id.tvSeat4);
                v.setTag(viewHolder);
            } else {
                v = convertView;
                viewHolder = (ViewHolder) v.getTag();
            }

            viewHolder.ticketName.setText(data.getName());
            viewHolder.ivFlagStart.setImageResource(data.isStartFlag() ? R.drawable.flg_shi : R.drawable.flg_guo);
            viewHolder.ivFlagEnd.setImageResource(data.isEndFlag() ? R.drawable.flg_zhong : R.drawable.flg_guo);
            viewHolder.tvStartTime.setText(data.getStartTime());
            viewHolder.tvEndTime.setText(data.getEndTime());


            List<String> seats = data.getSeats();
            for (int i = 0; i < seats.size(); i++) {
                String seatName = seats.get(i);
                switch (i) {
                    case 0:
                        viewHolder.tvSeat1.setText(seatName);
                        break;
                    case 1:
                        viewHolder.tvSeat2.setText(seatName);
                        break;
                    case 2:
                        viewHolder.tvSeat3.setText(seatName);
                        break;
                    case 3:
                        viewHolder.tvSeat4.setText(seatName);
                        break;
                }
            }

            return v;
        }

        class ViewHolder {
            TextView ticketName;
            ImageView ivFlagStart;
            ImageView ivFlagEnd;
            TextView tvStartTime;
            TextView tvEndTime;
            TextView tvSeat1;
            TextView tvSeat2;
            TextView tvSeat3;
            TextView tvSeat4;
        }
    }


}
